(function($){
    getLink('assets/common/vendors/zTree_v3/css/zTreeStyle/zTreeStyle.css');
    getScript('assets/common/vendors/zTree_v3/js/jquery.ztree.all-3.5.min.js');
    $(function(){
        function addHoverDom(treeId, treeNode) {
            var sObj = $("#" + treeNode.tId + "_span");
            if (!treeNode.isParent || $("#addBtn_"+treeNode.tId).length > 0) return;
            var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='新增' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            var btn = $("#addBtn_"+treeNode.tId);
            if (btn) btn.bind("click", function(){
                $('#menuEditForm').resetForm();
                var node = {isParent:false, parentId:treeNode.menuId, parentName:treeNode.menuName,menuId:''};
                $('#menuEditForm')._jsonToForm(node);
                $('#menuName').focus();
                return false;
            });
        }
        function removeHoverDom(treeId, treeNode) {
            $("#addBtn_"+treeNode.tId).unbind().remove();
        }
        var setting = {
            edit: {
                enable: true,
                renameTitle: '编辑',
                removeTitle: '删除',
                showRenameBtn: function(treeId, treeNode){if(treeNode.menuId == 0){return false;} else {return true;}},
                showRemoveBtn: function(treeId, treeNode){if(treeNode.menuId == 0){return false;} else {return true;}},
                drag:{isCopy : false,isMove : false}
            },
            data: {
                simpleData: { enable: true, idKey: "menuId", pIdKey: "parentId" },
                key : { name : "showName",url: "mUrl"},
                keep : {parent: true, leaf: true}
            },
            callback : {
                onClick : function(event, treeId, treeNode) {
                    if(treeNode.isParent) {
                        zTree.expandNode(treeNode, !treeNode.open, false, true);
                    }
                },
                beforeRemove:function(treeId, treeNode) {
                    zTree.selectNode(treeNode);
                    $.modalConfirm({
                        id:'modal-confirm',
                        content:'确定要删除该'+(treeNode.isParent ? '菜单及其子' : '')+'菜单吗？',
                        ok:function(){
                            $._ajaxPost(window.basePath + '/menu/del.html',{menuId:treeNode.menuId},function(r){
                                if(r.r) {
                                    zTree.removeNode(treeNode);
                                    $('#menuEditForm').resetForm();
                                }
                                asyncbox.tips(r.m,r.r ? 'success' : 'error');
                            });
                        }
                    });
                    return false;
                },
                beforeEditName : function(treeId, treeNode) {
                    if(treeNode.parentId == null)treeNode.parentId = 0;
                    $('#menuEditForm')._jsonToForm(treeNode);
                    var parent = treeNode.getParentNode();
                    $('#parentName').val(parent == null ? '无' : parent.showName);
                    zTree.selectNode(treeNode);
                    return false;
                }
            },
            view : {
                showTitle : false,
                selectedMulti : false,
                autoCancelSelected : false,
                addHoverDom : addHoverDom,
                removeHoverDom : removeHoverDom,
                fontCss : function(treeId, treeNode){
                    if(treeNode.status == 0){
                        return {color:"red"};
                    } else {
                        return {color:"black"};
                    }
                }
            }
        };
        $.fn.zTree.init($("#menuPanel"), setting, window.menus);
        var zTree = $.fn.zTree.getZTreeObj("menuPanel");
        zTree.expandAll(true);
        $('#menuEditForm').validator({
           theme:'yellow_right_effect',
           stopOnError: false,
           timely: 1,
           ignore: ':hidden',
           fields: {
               'menuName': {rule:'名称:required;length[2~20];'},
               'parentName' : '父菜单:required;'
           },
           valid: function(){
                var node = $('#menuEditForm')._formToJson();
                if($.trim(node.menuId) == '') {
                    $._ajaxPost(window.basePath + '/menu/add.html', node, function(r){
                        if(r.r) {
                            var parentNode = zTree.getNodeByParam('menuId', node.parentId, null);
                            node.menuId = r.menuId;
                            node.showName = $.trim(node.remark) == '' ? node.menuName : node.menuName + '[' + node.remark + ']';
                            zTree.addNodes(parentNode, node);
                            zTree.selectNode(zTree.getNodeByParam('menuId', node.menuId));
                            $('#menuEditForm').resetForm();
                        }
                        asyncbox.tips(r.m,r.r ? 'success' : 'error');
                    });
                } else {
                    $._ajaxPost(window.basePath + '/menu/upd.html', node, function(r){
                        if(r.r) {
                            var treeNode = zTree.getNodeByParam('menuId', node.menuId, null);
                            for(var key in node){
                                treeNode[key] = node[key];
                            }
                            treeNode.isParent = node.isParent == 'true' ? true : false;
                            treeNode.showName = $.trim(treeNode.remark) == '' ? treeNode.menuName : treeNode.menuName + '[' + treeNode.remark + ']';
                            zTree.updateNode(treeNode);
                            $('#menuEditForm').resetForm();
                        }
                        asyncbox.tips(r.m,r.r ? 'success' : 'error');
                    });
                }
           }
        });
    });
})(jQuery);
